<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta name="referrer" content="no-referrer"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>06.Python函数 | 扣小妖</title><meta name="author" content="扣小妖"><meta name="copyright" content="扣小妖"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Python入门课程  函数 章节目录 [!WARNING|label:本章需要学习的内容]   函数介绍 函数的定义 函数的参数 函数的返回值 函数说明文档 函数的嵌套调用 变量的作用域 综合案例   函数介绍 函数的定义  练习题代码  1234def welcome():    print(&quot;请出示您的健康码以及72小时核酸证明！&quot;) welcome()  函数的参数">
<meta property="og:type" content="article">
<meta property="og:title" content="06.Python函数">
<meta property="og:url" content="http://liukuaizhuan.gitee.io/posts/b2d84ecf.html">
<meta property="og:site_name" content="扣小妖">
<meta property="og:description" content="Python入门课程  函数 章节目录 [!WARNING|label:本章需要学习的内容]   函数介绍 函数的定义 函数的参数 函数的返回值 函数说明文档 函数的嵌套调用 变量的作用域 综合案例   函数介绍 函数的定义  练习题代码  1234def welcome():    print(&quot;请出示您的健康码以及72小时核酸证明！&quot;) welcome()  函数的参数">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://p.ananas.chaoxing.com/star3/origin/561b31d5498e9c25ac5416d4.png">
<meta property="article:published_time" content="2022-12-08T11:50:28.000Z">
<meta property="article:modified_time" content="2022-12-08T12:08:58.922Z">
<meta property="article:author" content="扣小妖">
<meta property="article:tag" content="笔记">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p.ananas.chaoxing.com/star3/origin/561b31d5498e9c25ac5416d4.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://liukuaizhuan.gitee.io/posts/b2d84ecf"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://gcore.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://gcore.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://gcore.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '06.Python函数',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-12-08 20:08:58'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://p.ananas.chaoxing.com/star3/origin/c6eb667caaea4b46a934d65b6a348821.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://p.ananas.chaoxing.com/star3/origin/561b31d5498e9c25ac5416d4.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">扣小妖</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">06.Python函数</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2022-12-08T11:50:28.000Z" title="发表于 2022-12-08 19:50:28">2022-12-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python%E5%9F%BA%E7%A1%80/">python基础</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="06.Python函数"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Python入门课程-函数"><a href="#Python入门课程-函数" class="headerlink" title="Python入门课程  函数"></a>Python入门课程  函数</h1><hr>
<h2 id="章节目录"><a href="#章节目录" class="headerlink" title="章节目录"></a><strong>章节目录</strong></h2><blockquote>
<p>[!WARNING|label:本章需要学习的内容]</p>
</blockquote>
<ul>
<li>函数介绍</li>
<li>函数的定义</li>
<li>函数的参数</li>
<li>函数的返回值</li>
<li>函数说明文档</li>
<li>函数的嵌套调用</li>
<li>变量的作用域</li>
<li>综合案例 </li>
</ul>
<h2 id="函数介绍"><a href="#函数介绍" class="headerlink" title="函数介绍"></a><strong>函数介绍</strong></h2><p><img src="https://img-blog.csdnimg.cn/45e4937cc5274369be14a78fcf7b95bb.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/43cf15e30fc94f65aab6bd1ba2997246.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/753144e9a0474ff193f0d16882f27dc7.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/76b88d31529042159c578c73e863f94f.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/bfdf36ed625b4d25a6c5111e605f2cbc.png" alt="5.jpg"></p>
<h2 id="函数的定义"><a href="#函数的定义" class="headerlink" title="函数的定义"></a><strong>函数的定义</strong></h2><p><img src="https://img-blog.csdnimg.cn/3ab4902d35ba4ea5b66604bce49d9a3c.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/2bb891be6f354795a728421657b245cb.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/38c4a5181bc04aba894b3cfc13666ba0.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/2b84c05ad4b94950b3aca45abaf1fcce.png" alt="5.jpg"></p>
<blockquote>
<p>练习题代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">welcome</span>():</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;请出示您的健康码以及72小时核酸证明！&quot;</span>) </span><br><span class="line"></span><br><span class="line">welcome() </span><br></pre></td></tr></table></figure>
<h2 id="函数的参数"><a href="#函数的参数" class="headerlink" title="函数的参数"></a><strong>函数的参数</strong></h2><p><img src="https://img-blog.csdnimg.cn/0f1bf69409964bde8102f08b8624f384.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/fac871395c57435ca2ab91b562cc478d.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/cfeee508a6874c948e004b9b53c65294.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/679e3e367f12455a85948bc83c9bccb4.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/c311e58d2a3c4c0f9052dda194f7340f.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/03ce2869c94c467b881991db7b380d3a.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/d0cc52decc0948da8a19b645c8e5ce0d.png" alt="7.jpg"></p>
<blockquote>
<p><strong>练习题代码</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">check</span>(<span class="params">num</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;请出示您的健康码以及72小时核酸证明,并配合测量体温&quot;</span>)</span><br><span class="line">    <span class="keyword">if</span> num &lt;= <span class="number">37.5</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;体温测试中，体温<span class="subst">&#123;num&#125;</span>,正常&quot;</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;体温<span class="subst">&#123;num&#125;</span>,需要隔离&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用函数</span></span><br><span class="line">check(<span class="number">37</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="函数的返回值"><a href="#函数的返回值" class="headerlink" title="函数的返回值"></a><strong>函数的返回值</strong></h2><ul>
<li>函数返回值的定义</li>
<li>None类型</li>
</ul>
<blockquote>
<p>[!tip|style:callout|label:①函数返回值的定义] </p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/4082a3d2480441af8095f4b075fb1c22.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/071d535088454372bb28dc56fdf8ad7e.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/400374ff629a4db68dbf7d8c4179bc0c.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/d30073923102423099ed0d82457bdbbd.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/cbf9abaabb164047a32b92cae23bef5a.png" alt="5.jpg"></p>
<blockquote>
<p>[!tip|style:callout|label:②None类型] </p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/55b3534aa5394acaa0161c03c112781a.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/9ad84e7e22234e5a83d70913caed0937.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/030caa22b5d4469e826fcd63fe57fc51.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/91c9f8d31b3c4243860c4151e2eb37dd.png" alt="4.jpg"></p>
<h2 id="函数说明文档"><a href="#函数说明文档" class="headerlink" title="函数说明文档"></a><strong>函数说明文档</strong></h2><p><img src="https://img-blog.csdnimg.cn/effafcb374b549f7bdc5533040651d92.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/506c4dde265246cdb845530b83f24b2a.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/a1173118249745249947c7da8dac3667.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/a6c775079b49488c937e750b6e2ae770.png" alt="4.jpg"></p>
<h2 id="函数的嵌套调用"><a href="#函数的嵌套调用" class="headerlink" title="函数的嵌套调用"></a><strong>函数的嵌套调用</strong></h2><p><img src="https://img-blog.csdnimg.cn/4ba05127306c4c009f02cb3331c98f5c.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/8dc8800b5de941419798d2ca4607ce7a.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/abdb572aab2f4137bcfb7e43085c36ee.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/8670dd4e6844483fa5517c66bc9be6e1.png" alt="4.jpg"></p>
<h2 id="变量的作用域"><a href="#变量的作用域" class="headerlink" title="变量的作用域"></a><strong>变量的作用域</strong></h2><p><img src="https://img-blog.csdnimg.cn/ef9147db707a4591aeffcd9d58998e88.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/878b953df45d490fad3dc02d449a6e44.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/6f3bad6da2ac4ca282402deb2bb3ab76.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/f5a8b0eea0e04687b43494f8c5ce39c1.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/64fd997a2d544e4896cc9fda0c0a7630.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/0a46741e1aad45478ed49f08bff1eb6b.png" alt="6.jpg"></p>
<h2 id="综合案例"><a href="#综合案例" class="headerlink" title="综合案例"></a><strong>综合案例</strong></h2><p><img src="https://img-blog.csdnimg.cn/46669225ef704d0f8a2c3ead1d03a6b4.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/a5eb8b6d726445fd8b905899650b5c2f.png" alt="2.jpg"></p>
<blockquote>
<p><strong>练习题 黑马ATM</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">函数的综合案例</span></span><br><span class="line"><span class="string">题目： 综合案例:黑马ATM</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">●定义一个全局变量: money,用来记录银行卡余额(默认5000000)</span></span><br><span class="line"><span class="string">●定义一个全局变量: name,用来记录客户姓名(启动程序时输入)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">定义如下的函数:</span></span><br><span class="line"><span class="string">    ●查询余额函数</span></span><br><span class="line"><span class="string">    ●存款函数</span></span><br><span class="line"><span class="string">    ●取款函数</span></span><br><span class="line"><span class="string">    ●主菜单函数</span></span><br><span class="line"><span class="string">要求:</span></span><br><span class="line"><span class="string">    ●程序启动后要求输入客户姓名</span></span><br><span class="line"><span class="string">    ●查询余额、存款、取款后都会返回主菜单</span></span><br><span class="line"><span class="string">    ●存款、取款后,都应显示一下当前余额</span></span><br><span class="line"><span class="string">    ●客户选择退出或输入错误,程序会退出，否则一直运行 </span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义一个全局变量: money,用来记录银行卡余额(默认5000000)</span></span><br><span class="line">money = <span class="number">5000000</span></span><br><span class="line"><span class="comment"># 定义一个全局变量: name,用来记录客户姓名(启动程序时输入)</span></span><br><span class="line">name = <span class="literal">None</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 程序启动后要求输入客户姓名</span></span><br><span class="line">name = <span class="built_in">input</span>(<span class="string">&quot;请输入您的姓名：&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># ●查询余额函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">query</span>(<span class="params">show_header</span>):</span><br><span class="line">    <span class="keyword">if</span> show_header:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;----------查询余额----------&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;【<span class="subst">&#123;name&#125;</span>】,您好，您的余额剩余：<span class="subst">&#123;money&#125;</span>元&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># ●存款函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">saving</span>(<span class="params">num</span>):</span><br><span class="line">    <span class="keyword">global</span> money  <span class="comment"># 全局</span></span><br><span class="line">    money += num</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;----------存款----------&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;【<span class="subst">&#123;name&#125;</span>】,您好，您存款<span class="subst">&#123;num&#125;</span>元成功。&quot;</span>)</span><br><span class="line">    <span class="comment"># 调用query函数查询余额</span></span><br><span class="line">    query(<span class="literal">False</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># ●取款函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_money</span>(<span class="params">num</span>):</span><br><span class="line">    <span class="keyword">global</span> money  <span class="comment"># 全局</span></span><br><span class="line">    money -= num</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;----------取款----------&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;【<span class="subst">&#123;name&#125;</span>】,您好，您取款<span class="subst">&#123;num&#125;</span>元成功。&quot;</span>)</span><br><span class="line">    <span class="comment"># 调用query函数查询余额</span></span><br><span class="line">    query(<span class="literal">False</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义主菜单函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">main</span>():</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;----------主菜单----------&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;【<span class="subst">&#123;name&#125;</span>】,您好，欢迎来到简易ATM。请选择操作:&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;查询余额\t[输入1]&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;存款\t\t[输入2]&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;取款\t\t[输入3]&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;退出\t\t[输入4]&quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">input</span>(<span class="string">&quot;请输入您的选择：&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置无线循环，确保程序不退出</span></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    keybord_input = main()</span><br><span class="line">    <span class="keyword">if</span> keybord_input == <span class="string">&quot;1&quot;</span>:</span><br><span class="line">        query(<span class="literal">True</span>)</span><br><span class="line">        <span class="keyword">continue</span>  <span class="comment"># 操作后，都会返回主菜单</span></span><br><span class="line">    <span class="keyword">elif</span> keybord_input == <span class="string">&quot;2&quot;</span>:</span><br><span class="line">        num = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;您想要存多少钱？请输入整数：&quot;</span>))</span><br><span class="line">        saving(num)</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    <span class="keyword">elif</span> keybord_input == <span class="string">&quot;3&quot;</span>:</span><br><span class="line">        num = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;您需要取出多少钱？请输入整数：&quot;</span>))</span><br><span class="line">        get_money(num)</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="comment"># 退出</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;程序退出啦~~~~&quot;</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://liukuaizhuan.gitee.io">扣小妖</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://liukuaizhuan.gitee.io/posts/b2d84ecf.html">http://liukuaizhuan.gitee.io/posts/b2d84ecf.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本站所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://p.ananas.chaoxing.com/star3/origin/561b31d5498e9c25ac5416d4.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://lib.baomitu.com/social-share.js/1.0.16/css/share.min.css" media="print" onload="this.media='all'"><script src="https://lib.baomitu.com/social-share.js/1.0.16/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://p.ananas.chaoxing.com/star3/origin/5a01db3c5a0daca9fa2b024736bf66e3.png" target="_blank"><img class="post-qr-code-img" src="https://p.ananas.chaoxing.com/star3/origin/5a01db3c5a0daca9fa2b024736bf66e3.png" alt="微信打赏"/></a><div class="post-qr-code-desc">微信打赏</div></li><li class="reward-item"><a href="https://p.ananas.chaoxing.com/star3/origin/afffb275198370656cb6cef6a1107185.png" target="_blank"><img class="post-qr-code-img" src="https://p.ananas.chaoxing.com/star3/origin/afffb275198370656cb6cef6a1107185.png" alt="支付宝打赏"/></a><div class="post-qr-code-desc">支付宝打赏</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/24e420f7.html"><img class="prev-cover" src="https://p.ananas.chaoxing.com/star3/origin/f284fb2dc534d3975a7733ab0eb843eb.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">07.Python数据容器</div></div></a></div><div class="next-post pull-right"><a href="/posts/faab6e1.html"><img class="next-cover" src="https://p.ananas.chaoxing.com/star3/origin/0df4f1a3cf2f76812e9cbf25c3190113.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">05.Python循环语句</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://p.ananas.chaoxing.com/star3/origin/c6eb667caaea4b46a934d65b6a348821.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">扣小妖</div><div class="author-info__description">Email：  liukuaizhuan@qq.com</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">14</div></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog 技术笔记</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Python%E5%85%A5%E9%97%A8%E8%AF%BE%E7%A8%8B-%E5%87%BD%E6%95%B0"><span class="toc-number">1.</span> <span class="toc-text">Python入门课程  函数</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AB%A0%E8%8A%82%E7%9B%AE%E5%BD%95"><span class="toc-number">1.1.</span> <span class="toc-text">章节目录</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.2.</span> <span class="toc-text">函数介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-number">1.3.</span> <span class="toc-text">函数的定义</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">1.4.</span> <span class="toc-text">函数的参数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-number">1.5.</span> <span class="toc-text">函数的返回值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E%E6%96%87%E6%A1%A3"><span class="toc-number">1.6.</span> <span class="toc-text">函数说明文档</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%B5%8C%E5%A5%97%E8%B0%83%E7%94%A8"><span class="toc-number">1.7.</span> <span class="toc-text">函数的嵌套调用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">1.8.</span> <span class="toc-text">变量的作用域</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B"><span class="toc-number">1.9.</span> <span class="toc-text">综合案例</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/7f4a6eeb.html" title="013.c++核心编程-类和对象"><img src="https://p.ananas.chaoxing.com/star3/origin/7ea674ffcd12ef7d943d54c93e9203a4.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="013.c++核心编程-类和对象"/></a><div class="content"><a class="title" href="/posts/7f4a6eeb.html" title="013.c++核心编程-类和对象">013.c++核心编程-类和对象</a><time datetime="2023-02-13T14:55:03.000Z" title="发表于 2023-02-13 22:55:03">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/4adebd64.html" title="012.C++核心编程-函数提高"><img src="https://p.ananas.chaoxing.com/star3/origin/b6c49bb4892ad191edbc0363caf45530.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="012.C++核心编程-函数提高"/></a><div class="content"><a class="title" href="/posts/4adebd64.html" title="012.C++核心编程-函数提高">012.C++核心编程-函数提高</a><time datetime="2023-02-13T14:51:24.000Z" title="发表于 2023-02-13 22:51:24">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/bff12c11.html" title="011.C++核心编程-引用"><img src="https://p.ananas.chaoxing.com/star3/origin/86403540a7b572fec123890e9a638adc.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="011.C++核心编程-引用"/></a><div class="content"><a class="title" href="/posts/bff12c11.html" title="011.C++核心编程-引用">011.C++核心编程-引用</a><time datetime="2023-02-13T14:47:34.000Z" title="发表于 2023-02-13 22:47:34">2023-02-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/9368cbfc.html" title="010.C++核心编程-内存分区模型"><img src="https://p.ananas.chaoxing.com/star3/origin/b4c7f6d417c4c75ed02262f3549625c8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="010.C++核心编程-内存分区模型"/></a><div class="content"><a class="title" href="/posts/9368cbfc.html" title="010.C++核心编程-内存分区模型">010.C++核心编程-内存分区模型</a><time datetime="2023-02-06T15:28:05.000Z" title="发表于 2023-02-06 23:28:05">2023-02-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/b056b485.html" title="009.C++基础实战-通讯录管理系统"><img src="https://p.ananas.chaoxing.com/star3/origin/5cdbcc7b75aea2aa3a8b12cae1439e99.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="009.C++基础实战-通讯录管理系统"/></a><div class="content"><a class="title" href="/posts/b056b485.html" title="009.C++基础实战-通讯录管理系统">009.C++基础实战-通讯录管理系统</a><time datetime="2023-02-04T10:02:31.000Z" title="发表于 2023-02-04 18:02:31">2023-02-04</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 扣小妖</div><div class="footer_custom_text"><p style="margin:10px 0 5px"><a target="_blank" href="https://hexo.io/" style="margin-inline:5px">博客 hexo</a><a target="_blank" href="https://butterfly.js.org/" style="margin-inline:5px">主题 Butterfly</a><a target="_blank" href="https://gitee.com/" style="margin-inline:5px">托管 Gitee</a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://gcore.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://gcore.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script></div><script defer="defer" id="ribbon" src="https://gcore.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>